﻿//================================================================================
// Filename: NavigationBarPlugin.as
// Authour: Jacky Fong
// Description:
// Handle navigation bar
//
// Input Events
// - NavigationBarPlugin_Load
// - NavigationBarPlugin_SetPosition
// - NavigationBarPlugin_Show
// - NavigationBarPlugin_SetLink
// - PagePlugin_ShowPageComplete
//
// Output Events
// - NavigationBarPlugin_LoadComplete
//================================================================================

package
{
	import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.net.URLRequest;	
	import flash.display.Sprite;
	import flash.events.*;
	import IPlugin;
	import PluginManager;
	import PluginEvent;
	
	public class NavigationBarPlugin extends Sprite implements IPlugin
	{
		private static const _pluginName:String = "NavigationBarPlugin";
		private var _manager:PluginManager;
		private var _loader:Loader; // loader as container
		private var _curPage:String = "";
		
		public function NavigationBarPlugin()
		{
			init();
		}
		
		public function get pluginName():String
		{
			return _pluginName;
		}
		
		public function get curPage():String
		{
			return _curPage;
		}
		
		public function get manager():PluginManager
		{
			return _manager;
		}
		
		public function init():void
		{
			trace( "Init " + _pluginName );
		}
		
		public function setManager( manager:PluginManager ):void
		{
			_manager = manager;
			_setupEventListeners();
		}		
		
		// listen to plugin's self operations
		private function _setupEventListeners():void
		{
			if( _manager == null ) return;
			
			_manager.addEventListener( "NavigationBarPlugin_Load", _onLoad );
			_manager.addEventListener( "NavigationBarPlugin_SetPosition", _onSetPosition );
			_manager.addEventListener( "NavigationBarPlugin_Show", _onShow );
			_manager.addEventListener( "NavigationBarPlugin_SetLink", _onSetLink );
			_manager.addEventListener( "PagePlugin_ShowPageComplete", _onShowPageComplete );
		}
		
        private function _loadComplete( evt:Event ):void
		{
			_manager.apiLog( this + " Load " + evt.target.content + " Complete: " + evt );
			evt.target.content.plugin = this; //assign self to NavigationBar ui
			_manager.dispatchEvent( new PluginEvent( "NavigationBarPlugin_LoadComplete" ) );
        }
		
		private function _loadIoError( evt:IOErrorEvent ):void
		{
			_manager.apiLog( this + " Load IoError: " + evt );
		}
				
		//==========================================================================
		// plugin API
		//==========================================================================		
		private function _onLoad( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onLoad: " + evt );
						
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _loadComplete );			
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _loadIoError );
            var req:URLRequest = new URLRequest( evt.args.url );
            _loader.load( req );
			_loader.visible = false;
			_manager.apiAddChild( _loader );
		}
		
		private function _onShow( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onShow: " + evt );
			_loader.visible = evt.args.show;
		}
		
		private function _onSetPosition( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onSetPosition: " + evt );
			_loader.x = evt.args.x;
			_loader.y = evt.args.y;			
		}
		
		private function _onSetLink( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onSetLink: " + evt );			
			var navBar:DisplayObject = _loader.content;
			if( navBar[ evt.args.button ] == undefined ) return;
			// use dynamic functions to read args from evt
			var func:Object = new Object();
			func.onMouseClick = function( mevt:Event )
			{
				_manager.dispatchEvent( new PluginEvent( "PagePlugin_ShowPage", { page:evt.args.page } ) );
			}			
			navBar[ evt.args.button ].addEventListener( MouseEvent.CLICK, func.onMouseClick );			
		}
		
		//==========================================================================
		// listen to other plugins' events
		//==========================================================================			
		private function _onShowPageComplete( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onShowPageComplete: " + evt );
			_curPage = evt.args.page;
		}
	}
}